// index.js
// 日记聚合页


import util from '../../../utils/util.js';
var app = getApp();

Page({

  data: {
    carlist:[],
  },

  jumpToMyCar() {
    wx.navigateTo({
      url: '../addcar/mycar'
    })
  },

  onShow: function () {
    this.getlist();

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

editDetail:function(obj){
  var that = this;
  var objkey = parseInt(obj.currentTarget.id);
  // for()
  var ret = this.data.carlist[objkey];

  // for (var i = 0; i < this.dcarlist.length; i++) {

  //   console.log(this.carlist[i].carcode);

  // }
  let data = JSON.stringify(ret);

    wx.navigateTo({
      url: '../addcar/mycar?data=' + data,
    });

},

setFav:function(obj)
{
  var that = this;
  var objkey = parseInt(obj.currentTarget.id);
  // for()
  var ret = this.data.carlist[objkey];
  wx.showModal({
    title: '提醒',
    content: '确定设置为默认吗？',
    success: function (eci) {
      if (eci.confirm) {
        that.favcatNet(ret);
      } else {

      }
    }
  });
},
  
  deleteCar: function (obj) {
   
   var that = this;

    var playingID = obj.currentTarget.id;
    wx.showModal({
      title: '提醒',
      content: '确定删除当前车辆吗？',
      success: function (eci) {
        if (eci.confirm) {
          that.delcatNet(playingID);
        } else {
          
        }
      }
    });

  },

  favcatNet: function (ret) {
    var url = app.globalData.SERVER+'car/addXcxNewCar';
    var that = this;
    // var usetinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    var tel = usetinfo.tel;
    util.request(url, 'POST', {
      isAdd:'0',
      tel: tel,
      carcode: ret.carcode,
      isdefault: '1',
      carbrandlogo: ret.carbrandlogo,
      carbrandname: ret.carbrandname,
      carbrandid: ret.carbrandid,
      carseriesname: ret.carseriesname,
      carseriesid: ret.carseriesid,
      cartypename: ret.cartypename,
      cartypeid: ret.cartypeid,
      totalkm: ret.totalkm,
      buytime: ret.buytime,
      vincode: ret.vincode,
    }).then(res => {
      // util.showToast('新增成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        // this.setData({
        //   carlist: err.data.ret,
        // });
        // var num = 1;
        util.showToast('修改成功', 'success', 1000);
        that.getlist();

        ret.isdefault = '1'
        util.saveData("defaultcar", ret);

      } else {
        util.showToast('修改失败', 'error', 1000);
      }


    });
  },

  
  delcatNet: function (playingID) {
    var url = app.globalData.SERVER+'car/delOneXcxCar';
    var that = this;
  
    // var userinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    var tel = usetinfo.tel;
    util.request(url, 'POST', {

      tel: tel,
      carcode:playingID,
    }).then(res => {
      // util.showToast('新增成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        // this.setData({
        //   carlist: err.data.ret,
        // });
        // var num = 1;
        util.showToast('删除成功', 'error', 2500);
        that.getlist();
      } else {
        util.showToast('删除失败', 'error', 2500);
      }

      
    });
  },


  getlist(){
    var url = app.globalData.SERVER+'car/getAllMyCars';

    // var tel = xcx_user_info.tel;
    // var tel = user.tel;
    // var userinfo = wx.getStorageSync("user_info");
    var userinfo = util.getData("userinfo");
    var tel = userinfo.tel;
    var i=1;
    util.request(url, 'POST', {
      
      tel: tel,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1 ) {
        console.log(err.data.ret);
        this.setData({
        carlist: err.data.ret,
        });
        var  num = 1;
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }

     
    });
  }
})
